package cz.muni.fi.pa165.mushroom.website.web;

import cz.muni.fi.pa165.mushroom.website.dto.MushroomDto;
import cz.muni.fi.pa165.mushroom.website.dto.MushroomTypeDTO;
import cz.muni.fi.pa165.mushroom.website.servicesImpl.MushroomServiceImpl;
import cz.muni.fi.pa165.mushroom.website.utils.Months;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.LocalizableMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.controller.StripesConstants;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * Action bean for mushrooms
 * 
 * @author Pavol Donko
 */
public class MushroomActionBean  extends BasicActionBean{
    
    @SpringBean
    protected MushroomServiceImpl mushroomServiceImpl;
    
    List<MushroomDto> mushrooms;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        //@Validate(on = {"add", "save"}, field = "growsFrom", required = true),
//        @Validate(on = {"add", "save"}, field = "growsUntil", required = true),
        @Validate(on = {"add", "save"}, field = "type", required = true)
    })
    private MushroomDto mushroomDto;   
    private MushroomTypeDTO MushroomTypeDto;
    
    private Months growsFrom;
    private Months growsUntil;
    
     List<MushroomDto> mushroomsVisit;
     List<MushroomDto> mushroomsUnreg;

    public Months getGrowsFrom() {
        return growsFrom;
    }

    public void setGrowsFrom(Months growsFrom) {
        this.growsFrom = growsFrom;
    }

    public Months getGrowsUntil() {
        return growsUntil;
    }

    public void setGrowsUntil(Months growsUntil) {
        this.growsUntil = growsUntil;
    }
    
     
     
    
    
    @DefaultHandler
    public Resolution all() {
        setMushrooms(mushroomServiceImpl.listMushrooms());
        return new ForwardResolution("/mushroomCatalog.jsp");
    }
    
    public Resolution add() { 
        mushroomDto.setGrowsFrom(growsFrom.getValue());
        mushroomDto.setGrowsUntil(growsUntil.getValue());
        mushroomServiceImpl.createMushroom(mushroomDto);
        getContext().getMessages().add(new LocalizableMessage("mushroom.add.message", escapeHTML(mushroomDto.getName()))); 
       setMushrooms(mushroomServiceImpl.listMushrooms());
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution delete() {  
        String name = mushroomDto.getName();
        mushroomServiceImpl.deleteMushroom(mushroomDto);  
        getContext().getMessages().add(new LocalizableMessage("mushroom.delete.message", escapeHTML(name)));
        return new RedirectResolution(this.getClass(), "all");
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadMushroomFromDatabase() {
        String ids = getContext().getRequest().getParameter("mushroomDto.id");
        if (ids == null) {
            return;
        }
        mushroomDto = mushroomServiceImpl.findMushroom(Long.parseLong(ids));
    }

    public Resolution edit() {
        return new ForwardResolution("/protected/editMushroom.jsp");
    }
 
    public Resolution save() {
        mushroomDto.setGrowsFrom(growsFrom.getValue());
        mushroomDto.setGrowsUntil(growsUntil.getValue());
        mushroomServiceImpl.updateMushroom(mushroomDto);
        getContext().getMessages().add(new LocalizableMessage("mushroom.update.message", escapeHTML(mushroomDto.getName())));
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution cancel(){
        return new RedirectResolution(this.getClass(), "all"); 
    }
 
    public Resolution searchType() {
        String type = (String) getContext().getRequest().getParameter("type");        
        setMushrooms(mushroomServiceImpl.listMushroomsWithType(MushroomTypeDto.valueOf(type)));
        return new ForwardResolution("/mushroomCatalog.jsp");
    }
    
    public Resolution searchByName(){
        String name = (String) getContext().getRequest().getParameter("filter.name");                
        setMushrooms(mushroomServiceImpl.listMushroomsWithName(name));        
        return new ForwardResolution("/mushroomCatalog.jsp");
    }
    
    public Resolution showMushrooms(){
        setMushrooms(mushroomServiceImpl.listMushrooms());        
        return new ForwardResolution("/mushroomCatalog.jsp");        
    }
    
    public MushroomDto getMushroomDto() {
        return mushroomDto;
    }
    
    public void setMushroomDto(MushroomDto mushroomDto) {
        this.mushroomDto = mushroomDto;
    }
    
    public List<MushroomDto> getMushrooms() {
        if(mushrooms == null) {
            return mushroomServiceImpl.listMushrooms();
        }
        else return mushrooms;
    }
    
    public void setMushrooms(List<MushroomDto> mushrooms) {
        this.mushrooms = mushrooms;
    }    

    public List<MushroomDto> getMushroomsVisit() {
        mushroomsVisit = mushroomServiceImpl.listMushrooms();
        return mushroomsVisit;
    }

    public void setMushroomsVisit(List<MushroomDto> mushroomsVisit) {
        this.mushroomsVisit = mushroomsVisit;
    }

    public List<MushroomDto> getMushroomsUnreg() {
        return mushroomServiceImpl.listMushrooms();
    }

    public void setMushroomsUnreg(List<MushroomDto> mushroomsUnreg) {
        this.mushroomsUnreg = mushroomsUnreg;
    }
    
    
}